﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

using Microsoft.VisualStudio.CommandBars;
using EnvDTE;
using EnvDTE80;

namespace VSAddInFramework
{
    // This class is used to group related commands together, and put them on a same toolbar
    public class VSCommandGroup
    {
        public VSCommandGroup(String toolbarName)
        {
            m_ToolBarName = toolbarName;
        }

        public void AddCommand(VSCommand cmd)
        {
            Debug.Assert(cmd != null);
            if (cmd != null)
            {
                m_Commands.Add(cmd);
            }
        }

        // Create a toolbar and add commands to the toolbar.
        public bool CreateCommandsUI()
        {
            CommandBars cmdBars = VSAddInMgr.Instance.Application.CommandBars as CommandBars;
            Debug.Assert(cmdBars != null);

            m_CommandBar = cmdBars.Add(m_ToolBarName, MsoBarPosition.msoBarTop, System.Type.Missing, true);
            Debug.Assert(m_CommandBar != null);

            int index = 0;
            foreach (VSCommand cmd in m_Commands)
            {
                cmd.InternalCommand.AddControl(m_CommandBar, ++index);
            }

            m_CommandBar.Visible = true;

            return true;
        }

        public bool DestroyCommandsUI()
        {
            if (m_CommandBar != null)
            {
                m_CommandBar.Delete();
            }
            return true;

        }


        public List<VSCommand> VSCommands
        {
            get
            {
                return m_Commands;
            }
        }

        private List<VSCommand> m_Commands = new List<VSCommand>();
        private String m_ToolBarName;
        private CommandBar m_CommandBar;
    }
}
